﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace Schüleranmeldung.Mail
{
    class MappingHelper
    {
        private Dictionary<string, MappingInfo> mappingInfo;

        public void LoadMappingTable()
        {
            string xml = File.ReadAllText("mailmapping.xml");
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            mappingInfo = new Dictionary<string, MappingInfo>();
            foreach (XmlNode tableNode in doc.SelectSingleNode("Mapping").ChildNodes)
            {
                foreach (XmlNode column in tableNode.ChildNodes)
                {
                    MappingInfo info = new MappingInfo();
                    info.Table = tableNode.Attributes["name"].Value;
                    info.ColumnName = column.Name;
                    XmlAttribute mapping = column.Attributes["mapping"];
                    if (mapping != null)
                        mappingInfo.Add(mapping.Value, info);
                    else
                        mappingInfo.Add(column.Name, info);
                }
            }
        }

        public MappingInfo GetMappingInfo(string name)
        {
            if (mappingInfo.ContainsKey(name))
                return mappingInfo[name];
            return null;
        }
    }
}
